import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.util.List;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
    // test0();
     //test1();
        // test2();
       // test3();
       test4();
        //test5();
    }

    private static void test5() throws InterruptedException { //找到图片这个元素然后右击
               WebDriver webDriver = new ChromeDriver();
               webDriver.get("https://www.baidu.com");
               webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件工程"); //键盘输入软件工程
               webDriver.findElement(By.cssSelector("#su")).click(); //找到搜索按钮然后点击
               Thread.sleep(1000);
               WebElement element = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
               //
        Actions actions = new Actions(webDriver);
        actions.moveToElement(element).contextClick().perform();

    }

    private static void test4() throws InterruptedException {
        //对浏览器的操作
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件工程");
        Thread.sleep(3000);

        webDriver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        //浏览器的后退
        webDriver.navigate().back();
        Thread.sleep(3000);
        //浏览器的刷新
        webDriver.navigate().refresh();
        Thread.sleep(3000);
        //浏览器的前进
        webDriver.navigate().forward();
        Thread.sleep(3000);
        //这个是对浏览器中滚动条的操作
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        Thread.sleep(1000);
        webDriver.manage().window().fullscreen();
        Thread.sleep(1000);
        webDriver.manage().window().setSize(new Dimension(600,900));


    }

    private static void test3() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        String s=webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
        if("百度一下".equals(s)){
            System.out.println("测试通过");
        }else{
            System.out.println("测试不通过");
        }
    }

    private static void test2() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //这里不能用submit，只能用click，因为submit只有在form标签下，效果才能和click一样，
        //此时这里的元素（百度首页新闻）是一个a标签，不是form标签下，所以不能代替click使用，否则会报错
        webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).click();
    }

    private static void test1() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("好运来");
        webDriver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        //清空百度输入框中的内容
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    public static void test0() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //找到百度搜索框
        // WebElement element =  webDriver.findElement(By.cssSelector(".s_ipt"));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //输入要搜索的内容
        element.sendKeys("软件测试");
        //找到百度一下按钮，点击
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(1000);
        //检验搜索的结果(校验）
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i <webElements.size(); i++) {
            if(webElements.get(i).getText().equals("软件测试")){
                System.out.println("测试成功");
                return;
            }
        }

        System.out.println("测试失败");

    }
}